//
//  LHWelcomController.swift
//  sina_weibo
//
//  Created by 刘刘海强 on 16/5/13.
//  Copyright © 2016年 chuanzhi. All rights reserved.
//

import UIKit

class LHWelcomController: UIViewController {
    
    //北京图片
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //头像
    private lazy var headerImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    //标题
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        if let name = LHUserAccountModel.sharedUserAccount.userAccount?.name {
            label.text = name + ", 欢迎回来"
        } else {
            label.text = "欢迎回来"
        }
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        return label
    }()
    
    override func loadView() {
        view = bgImageView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    private func setupUI() {
        view.addSubview(headerImageView)
        view.addSubview(messageLabel)
        
        headerImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.top.equalTo(self.view).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headerImageView)
            make.top.equalTo(headerImageView.snp_bottom).offset(10)
        }
        
    }
    
    private func startAnimation() {
        headerImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        }
        messageLabel.alpha = 0
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //进入首页
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: nil)
                })
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }


}
